<?php
class ctrl_interview extends ctrl_base
{
    public static function index() {
    	self::weixin_check_login();
    	self::smarty() -> assign('user_type',$_SESSION['wx_userinfo']['user_type']);
    	self::smarty() -> display('interview_index.html');
    }
    //判断当前用户是否是内部用户
    private static function is_sales_user() {
   		self::weixin_check_login();
    	if($_SESSION['wx_userinfo']['user_type'] != 1) { //非内部用户
    		echo "<script>alert('您好！此服务仅供日钢业务员使用，请先注册成为内部用户！');document.location.href = '/index.php?act=register';</script>";
    		exit;
    	}
    }
    
    //售前走访信息录入页面
    public static function pre_sales() {
    	self::is_sales_user();
    	self::smarty() -> display('interview_pre_sales.html');
    }
    //保存售前走访信息
    public static function presalesSave() {
    	self::is_sales_user();
    	$data = array();
    	$data['sales_id'] = $_SESSION['wx_userinfo']['openid'];
    	$data['visit_unit'] = trim($_POST['visit_unit']);
    	if(empty($data['visit_unit'])) {
    		$errMsg = '走访单位名称不为空!';
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",$errMsg);
    		self::smarty()->display('show_result.html');
    		exit;
    	}
    	$data['visit_addr'] = trim($_POST['visit_addr']);
    	if(empty($data['visit_addr'])) {
    		$errMsg = '用户详细地址不为空!';
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",$errMsg);
    		self::smarty()->display('show_result.html');
    		exit;
    	}
    	$data['recevie_name'] = trim($_POST['recevie_name']);
    	if(empty($data['recevie_name'])) {
    		$errMsg = '接待人姓名不为空!';
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",$errMsg);
    		self::smarty()->display('show_result.html');
    		exit;
    	}
    	$data['recevie_study'] = trim($_POST['recevie_study']);
    	$data['recevie_mobile'] = trim($_POST['recevie_mobile']);
    	$data['recevie_email'] = trim($_POST['recevie_email']);
    	$data['recevie_facts'] = trim($_POST['recevie_facts']);
    	$data['recevie_question'] = trim($_POST['recevie_question']);
    	$data['recevice_mind'] = trim($_POST['recevice_mind']);
    	$data['recevie_date'] = date('Y-m-d H:i:s');

    	$reult = model_interview::add_interview_record($data);
    	if($reult > 0) {
    		self::smarty()->assign("result_class",'icon67_status');
    		self::smarty()->assign("errMsg",'客户售前走访信息录入成功！');
    		self::smarty()->display('show_result.html');
    	} else {
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",'信息录入失败！');
    		self::smarty()->assign("comment",'系统异常，请稍后重试！');
    		self::smarty()->display('show_result.html');
    	}
    }
    //售后走访信息录入页面
    public static function aftermarket() {
   		self::is_sales_user();
    	self::smarty() -> display('interview_aftermarket.html');
    }
	//保存售后走访信息
    public static function aftermarketSave() {
    	self::is_sales_user();
    	$customer_info = array();
    	//$data['sales_id'] = $_SESSION['wx_userinfo']['openid'];
    	$customer_info['customer_name'] = trim($_POST['customer_name']);
    	if(empty($customer_info['customer_name'])) {
    		$errMsg = '客户名称不为空!';
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",$errMsg);
    		self::smarty()->display('show_result.html');
    		exit;
    	}
    	$customer_info['province'] = trim($_POST['province']);    	
    	$customer_info['city'] = trim($_POST['city']);
    	$customer_info['area'] = trim($_POST['area']);
    	$customer_info['street'] = trim($_POST['street']);
    	$customer_info['general'] = trim($_POST['general']);    	
    	$customer_info['ability'] = trim($_POST['ability']);
    	$customer_info['status'] = trim($_POST['status']);
    	$customer_info['part'] = trim($_POST['part']);
    	$customer_info['sale_name'] = trim($_POST['sale_name']);
    	$customer_info['author'] = $_SESSION['wx_userinfo']['openid'];
    	$customer_info_id = model_interview::add_interview_record($customer_info,'weixin_customer_info');
    	if($customer_info_id == 0) {
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",'客户信息录入失败！');
    		self::smarty()->assign("comment",'系统异常，请稍后重试！');
    		self::smarty()->display('show_result.html');
    		exit;
    	}
    	//问题描述
    	$qs['question_list'] = $_POST['question'];
    	$qs['question_desc_list'] = $_POST['question_desc'];
    	$question_count = count($qs['question_list']);
    	if($question_count == 1) {
    		$question['customer_info_id'] = $customer_info_id;
    		$question['question'] = trim($qs['question_list'][0]);
    		$question['question_desc'] = trim($qs['question_desc_list'][0]);
    		if(!empty($question['question'])) {
	    		$reult = model_interview::add_interview_record($question,'weixin_customer_question');
    		}
    	} elseif ($question_count > 1) {
    		for($i=0;$i<$question_count;$i++) {
	    		$question['customer_info_id'] = $customer_info_id;
	    		$question['question'] = trim($qs['question_list'][$i]);
	    		$question['question_desc'] = trim($qs['question_desc_list'][$i]);
	    		if(!empty($question['question'])) {
		    		$reult = model_interview::add_interview_record($question,'weixin_customer_question');
	    		}
    		}
    	}
    	//收集人信息
    	$data['customer_info_id'] = $customer_info_id;
    	$data['collection_date'] = trim($_POST['collection_date']);
    	$data['collection_channel'] = trim($_POST['collection_channel']);    	
    	$data['collection_name'] = trim($_POST['collection_name']);
    	$data['collection_part'] = trim($_POST['collection_part']);
    	$data['collection_tel'] = trim($_POST['collection_tel']);
    	
    	$data['agreement'] = trim($_POST['agreement']);
    	$data['product'] = trim($_POST['product']);    	
    	$data['grade'] = trim($_POST['grade']);
    	$data['format'] = trim($_POST['format']);
    	if(isset($_POST['number']) && trim($_POST['number']) != '') {
    		$data['number'] = trim($_POST['number']).'吨';
    	} else {
    		$data['number'] = '';
    	}
    	$data['betch'] = trim($_POST['betch']);
    	$data['occur_date'] = trim($_POST['occur_date']);    	
    	$data['person_name'] = trim($_POST['person_name']);
    	$data['person_study'] = trim($_POST['person_study']);
    	$data['person_tel'] = trim($_POST['person_tel']);
    	
    	$reult = model_interview::add_interview_record($data,'weixin_question_info');
    	if($reult == 0) {
    		self::smarty()->assign("result_class",'icon67_status warn');
    		self::smarty()->assign("errMsg",'客户售后走访信息录入失败！');
    		self::smarty()->assign("comment",'系统异常，请稍后重试！');
    		self::smarty()->display('show_result.html');
    		exit;
    	} else {
    		self::smarty()->assign("result_class",'icon67_status');
    		self::smarty()->assign("errMsg",'客户售后走访信息录入成功！');
    		self::smarty()->display('show_result.html');
    	}
    }
}
